import re

m = re.search(r'\d{2,}','1 set of 23 owls, 999 doves')
print('"',m.group(),'" found at ', m.span(),sep='')

s = '1 set of 23 owls, 999 doves'
for i in re.finditer(r'\d+',s):
    print(i.group())

ss = 'What is 1,000.5 times 3 times 2,000?'
print(re.findall(r'\d[0-9,.]*',ss))   # 指定第一个必须为数字

s1 = '12 15+3 100-*'
re.findall(r'[+*/-]|\w+',s1)  # 负号（-）在方括号中具有特殊的含义，除非它出现在字符集的开始或末尾

patt = r'(\d{1,3}(,\d{3})*(\.\d*)?)'   # ? 表示匹配 零次或 一次 为一个可选项
lst = re.findall(patt,'12,000 monkeys on 55.5 cats.')
for item in lst:
    print(item)

print('----------------------------------')
# 搜索重复模式，比如两个重复的单词
s2 = 'The cow jumped over the the moon.'
m2 = re.search(r'(\w+) \1',s2)
print(m2.group(),'...found at',m2.span())

s3 = 'The the cow jumped over the the moon.'
m3 = re.search(r'(\w+) \1',s3,flags=re.I)  # re.I 匹配项不再区分大小写
print(m3.group(),'...found at',m3.span())

a = 'Get me a new dog to befriend my dog.'
a1 = re.sub('dog','cat',a)   # 把 dog 换成 cat

a2 = 'The the cow jumped over over moon.'
sa2 = re.sub(r'(\w+) \1',r'\1',a2,flags=re.I)
print(sa2)



